package io.github.junxworks.qt.modules.mm.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.github.junxworks.ep.sys.constants.RecordStatus;
import io.github.junxworks.junx.stat.datawindow.DataWindowConstants;
import io.github.junxworks.junx.stat.function.FuncDef;
import io.github.junxworks.qt.modules.mm.entity.MmVariable;
import io.github.junxworks.qt.modules.mm.mapper.DatasourceMapper;

@Service("CreateStatService")
public class CreateStatService {

	public static final int[] peroid = new int[] { 5, 10, 20, 30, 60 };

	@Autowired
	private DatasourceMapper datasourceMapper;

	/**
	 * Creates the exp stat.
	 * name,stat_name,keyword,function
	 * @param param the param
	 */
	public void createExpStat(String param) {
		String[] params = param.split(",");
		String name = params[0];
		String stat_name = params[1];
		String key = params[2];
		String func = params[3];
		for (int i = 0, len = peroid.length; i < len; i++) {
			int size = peroid[i];
			MmVariable variable = new MmVariable();
			variable.setStatus(RecordStatus.NORMAL.getValue());
			variable.setCreateUser(-1L);
			variable.setCreateTime(new Date());
			variable.setName(size + name);
			variable.setDataWindowType(DataWindowConstants.WIN_TYPE_PRIORITY + "");
			variable.setDataWindowSize(size);
			variable.setStatFunc(func);
			variable.setExpression("return 指标('" + stat_name + "');");
			variable.setKeyword(key);
			variable.setOutputType("java.lang.Double");
			variable.setPriority(6);
			variable.setPriorityStrategy("FIFO");
			variable.setType("2");
			datasourceMapper.insertWithoutNull(variable);
		}
	}

	public void createDayAvgRatioStat(String param) {
		String[] params = param.split(",");
		String dsName = params[0];
		String field = params[1];
		for (int i = 0, len = peroid.length; i < len; i++) {
			int size = peroid[i];
			MmVariable variable = new MmVariable();
			variable.setStatus(RecordStatus.NORMAL.getValue());
			variable.setCreateUser(-1L);
			variable.setCreateTime(new Date());
			variable.setName(size +"日"+ field+"日均比");
			variable.setDataWindowType(DataWindowConstants.WIN_TYPE_PRIORITY + "");
			variable.setDataWindowSize(1);
			variable.setStatFunc(FuncDef.SNAPSHOT);
			variable.setExpression("return 数据流('"+dsName+"').get('"+field+"')/指标('"+size+"日平均" + field + "');");
			variable.setKeyword("ts_code");
			variable.setOutputType("java.lang.Double");
			variable.setPriority(7);
			variable.setPriorityStrategy("FIFO");
			variable.setType("2");
			datasourceMapper.insertWithoutNull(variable);
		}
	}

	public void createNromalStat(String param) {
		String[] params = param.split(",");
		String dsId = params[0];
		String field = params[1];
		String key = params[2];
		for (int i = 0, len = peroid.length; i < len; i++) {
			int size = peroid[i];
			MmVariable variable = new MmVariable();
			variable.setStatus(RecordStatus.NORMAL.getValue());
			variable.setCreateUser(-1L);
			variable.setCreateTime(new Date());
			variable.setName(size + "日平均" + field);
			variable.setDataWindowType(DataWindowConstants.WIN_TYPE_PRIORITY + "");
			variable.setDsId(Long.valueOf(dsId));
			variable.setStatField(field);
			variable.setDataWindowSize(size);
			variable.setStatFunc("avg");
			variable.setKeyword(key);
			variable.setOutputType("java.lang.Double");
			variable.setPriority(5);
			variable.setPriorityStrategy("FIFO");
			variable.setType("1");
			datasourceMapper.insertWithoutNull(variable);
			variable = new MmVariable();
			variable.setStatus(RecordStatus.NORMAL.getValue());
			variable.setCreateUser(-1L);
			variable.setCreateTime(new Date());
			variable.setName(size + "日" + field + "趋势");
			variable.setDataWindowType(DataWindowConstants.WIN_TYPE_PRIORITY + "");
			variable.setDsId(Long.valueOf(dsId));
			variable.setStatField(field);
			variable.setDataWindowSize(size);
			variable.setStatFunc("liner_regression");
			variable.setKeyword(key);
			variable.setOutputType("java.lang.Double");
			variable.setPriority(5);
			variable.setPriorityStrategy("FIFO");
			variable.setType("1");
			datasourceMapper.insertWithoutNull(variable);
		}
	}
}
